package com.zimmur.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.dao.IAccountInfoDao;
import com.zimmur.model.account.AccountInfo;
import com.zimmur.model.account.UserjeLog;
import com.zimmur.service.IAccountInfoService;
import com.zimmur.service.impl.account.UserjeLogServiceImpl;
import com.zimmur.tools.DateUtils;

@Service("accountInfoService")
public class AccountInfoServiceImpl implements IAccountInfoService {
	@Resource
	private IAccountInfoDao accountDao;
	@Resource
	private UserjeLogServiceImpl userjeLogService;
	
	@Transactional(readOnly=true)
	public AccountInfo selectByUserId(long userId) {
		return accountDao.selectByUserId(userId);
	}

	@Transactional(isolation=Isolation.READ_COMMITTED,rollbackFor = {RuntimeException.class})
	public boolean updateCurJeByUserId(long userId,double je,String remark) {
		if (je == 0) {
			return false;
		}
		try {
			AccountInfo accountInfo = selectByUserId(userId);
			if (accountInfo == null) {
				return false;
			}
			double curJe = accountInfo.getCurJe();
			if (je < 0 && curJe < je) {
				return false;
			}
			Map<String, Object> map = new HashMap<>();
			if (je > 0) {
				map.put("curJe", curJe + je);
			} else {
				map.put("curJe", curJe - je);
			}

			map.put("userId", userId);

			return accountDao.updateCurJeByUserId(map) > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		
	}

	@Transactional(isolation=Isolation.READ_COMMITTED,rollbackFor = {RuntimeException.class})
	public boolean updateCurPointByUserId(long userId,int point,String remark) {
		if (point == 0) {
			return false;
		}
		try {
			AccountInfo accountInfo = selectByUserId(userId);
			if (accountInfo == null) {
				return false;
			}
			int curPoint = accountInfo.getCurPoint();
			if (point < 0 && curPoint + point > 0) {
				return false;
			}
			Map<String, Object> map = new HashMap<>();
			map.put("curPoint", curPoint + point);

			map.put("userId", userId);
			return accountDao.updateCurPointByUserId(map) > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public Double selectCurJeByUserId(long userId) {
		AccountInfo account= accountDao.selectByUserId(userId);
		return account.getCurJe();
	}

	@Transactional(isolation=Isolation.READ_COMMITTED,rollbackFor = {RuntimeException.class})
	public boolean mainUseJeToUse(long userId, double useJe, String remark) {
		AccountInfo account= accountDao.selectByUserId(userId);
		if(useJe>0 && account.getCurJe()>=useJe){
			try{
				double curJe = account.getCurJe();
				double curUseJe = account.getUseJe();
				curJe = curJe - useJe;
				curUseJe = curUseJe + useJe;
				Map<String, Object> map = new HashMap<>();
				map.put("userId", userId);
				map.put("curJe", curJe);
				map.put("useJe", curUseJe);
				return accountDao.mainCurJeToUse(map) > 0 ? true : false;
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
		}
		return false;
	}

	@Transactional(isolation=Isolation.READ_COMMITTED,rollbackFor = {RuntimeException.class})
	public boolean mainUsePointToUse(long userId, int usePoint, String remark) {
		AccountInfo account= accountDao.selectByUserId(userId);
		if(usePoint>0 && account.getCurPoint()>=usePoint){
			try{
			Map<String, Object> map=new HashMap<>();
			map.put("userId", userId);
			map.put("usePoint", usePoint);
			return accountDao.mainCurJeToUse(map)>0?true:false;
			}catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
		}
		return false;
	}

	@Transactional(isolation=Isolation.READ_COMMITTED,rollbackFor = {RuntimeException.class})
	public boolean updateBackUseJeToCurJe(long userId, double useJe) {
		AccountInfo account = accountDao.selectByUserId(userId);
		if (useJe > 0 && account.getUseJe() >= useJe) {
			try {
				Map<String, Object> map = new HashMap<>();
				map.put("userId", userId);
				map.put("useJe", useJe);
				return accountDao.updateBackUseJeToCurJe(map) > 0 ? true : false;
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
		}
		return false;
	}

	@Transactional(isolation=Isolation.READ_COMMITTED,rollbackFor = {RuntimeException.class})
	public boolean updateUseJe(long userId, double useJe,String remark) {
		AccountInfo account= accountDao.selectByUserId(userId);
		if(useJe>0 && account.getUseJe()>=useJe){
			try{
			Map<String, Object> map=new HashMap<>();
			map.put("userId", userId);
			map.put("useJe", useJe);
			if(accountDao.updateUseJe(map)>0?true:false){
				//余额消费日志
				UserjeLog userjeLog = new UserjeLog();
				userjeLog.setUserId(userId);
				userjeLog.setEvent("-");
				userjeLog.setEventDate(DateUtils.getNowUnixTime());
				userjeLog.setJe(useJe);
				userjeLog.setRemark(remark);
				
				return userjeLogService.insert(userjeLog);
				
			}
			}catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
		}
		return false;
	}

	@Transactional(isolation=Isolation.READ_COMMITTED,rollbackFor = {RuntimeException.class})
	public boolean updateUsePoint(long userId, int usePoint,String remark) {
		AccountInfo account= accountDao.selectByUserId(userId);
		if(usePoint>0 && account.getUsePoint()>=usePoint){
			try{
			Map<String, Object> map=new HashMap<>();
			map.put("userId", userId);
			map.put("usePoint", usePoint);
			return accountDao.updateUsePoint(map)>0?true:false;
			}catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
		}
		return false;
	}

}
